package com.pet.server.ext.action.subject;

import java.util.ArrayList;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Result;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.Article;
import com.pet.server.ext.data.Constant;
import com.pet.server.ext.data.Page;
import com.pet.server.ext.data.Subject;
import com.pet.server.ext.service.ArticleService;
import com.pet.server.ext.util.JsonUtil;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;

public class List extends ActionBase{

	public Integer startIndex,itemCount,subject_id;
	public String queryString;
	public Page page;
	public List(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		startIndex = NumberUtil.getInteger(params.getFieldValue("startIndex"), 0);
		itemCount =  NumberUtil.getInteger(params.getFieldValue("itemCount"), 10);
		queryString = params.getFieldValue("queryString");
		subject_id=NumberUtil.getInteger(params.getFieldValue("subject_id"));
		page = new Page(startIndex, itemCount,queryString);
	}
	
	@Override
	public Result invokeService() throws ApiError {
		ArticleService articleService = new ArticleService();
		java.util.List<Constant> list = articleService.queryConstant(BaseAttribute.article_pcode);
		for(Constant c:list){
			c.setList(articleService.queryConstant(c.getCode()));
		}
		articleService.searchSubject(page);
		
	    JSONArray array = new JSONArray();
		for(Subject subject:(java.util.List<Subject>)page.getList()){
           JSONObject o = new JSONObject();
           o.put("id", subject.id);
           o.put("ico_path", subject.ico_path);
           o.put("title", subject.title);
           o.put("description", subject.description);
           o.put("create_time", JsonUtil.getTime(subject.created_time));
           array.add(o);
        }
			
		Result result = new Result();
        result.setPage4array(page, array);
	    return result;
	}


}
